//
//

#import "CellForWorkGroup.h"
#import "YHWorkGroupPhotoContainer.h"
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"
#import "AdjustHeight.h"
/***发布动态视图**/
CGFloat maxContentRepostLabelHeight;

const CGFloat contentLabelFontSize = 14;
CGFloat maxContentLabelHeight = 0;  //根据具体font而定
CGFloat kMarginContentLeft    = 10; //动态内容左边边距
CGFloat kMarginContentRight   = 10; //动态内容右边边边距

const CGFloat deleteBtnHeight = 30;
const CGFloat deleteBtnWidth  = 60;
const CGFloat moreBtnHeight   = 30;
const CGFloat moreBtnWidth    = 60;

@interface CellForWorkGroup()
@property (nonatomic,strong)UIImageView *imgvAvatar;
@property (nonatomic,strong)UILabel     *labelName;
@property (nonatomic,strong)UILabel     *labelPubTime;
@property (nonatomic,strong)UILabel     *labelContent;
@property (nonatomic,strong)UILabel     *labelDelete;
@property (nonatomic,strong)UILabel     *labelMore;
@property (nonatomic,strong)UILabel     *labelBottomLeft;
@property (nonatomic,strong)UILabel     *labelBottomRight;
@property (strong,nonatomic)UIView      *bottomView;
@property (strong,nonatomic)UIView      *lineView;


@property (nonatomic,strong)YHWorkGroupPhotoContainer *picContainerView;
@property (nonatomic,strong)UIView      *viewSeparator;

//约束
@property (nonatomic,strong)NSLayoutConstraint *cstHeightlbMore;
@property (nonatomic,strong)NSLayoutConstraint *cstHeightlbDelete;
@property (nonatomic,strong)NSLayoutConstraint *cstCenterYlbDelete;
@property (nonatomic,strong)NSLayoutConstraint *cstLeftlbDelete;
@property (nonatomic,strong)NSLayoutConstraint *cstHeightlbContent;
@property (nonatomic,strong)NSLayoutConstraint *cstHeightPicContainer;
@property (nonatomic,strong)NSLayoutConstraint *cstTopPicContainer;
@property (nonatomic,strong)NSLayoutConstraint *cstTopViewBottom;
@end

@implementation CellForWorkGroup

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setup];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setup{
    
    self.imgvAvatar = [UIImageView new];
    //self.imgvAvatar.layer.cornerRadius = 22.5;
    self.imgvAvatar.layer.masksToBounds = YES;
    self.imgvAvatar.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGuesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onAvatar:)];
    [self.imgvAvatar addGestureRecognizer:tapGuesture];
    [self.contentView addSubview:self.imgvAvatar];
    
    self.labelName  = [UILabel new];
    self.labelName.font = [UIFont systemFontOfSize:16.0f];
    self.labelName.textColor = UIColors(0x576b95);
    [self.contentView addSubview:self.labelName];
    
    self.labelPubTime = [UILabel new];
    self.labelPubTime.font = [UIFont systemFontOfSize:13.0f];
    self.labelPubTime.textColor = [UIColor grayColor];
    [self.contentView addSubview:self.labelPubTime];
    
    self.labelContent = [UILabel new];
    self.labelContent.font = [UIFont systemFontOfSize:contentLabelFontSize];
    self.labelContent.numberOfLines = 0;
//    self.labelContent.font = [UIFont systemFontOfSize:16.0f];
    [self.contentView addSubview:self.labelContent];
    
    self.labelDelete = [UILabel new];
    self.labelDelete.font = [UIFont systemFontOfSize:14.0f];
    self.labelDelete.textColor = [UIColor redColor];
    self.labelDelete.userInteractionEnabled = YES;
    UITapGestureRecognizer *deleteTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(deleteTap)];
    [self.labelDelete addGestureRecognizer:deleteTap];
    [self.contentView addSubview:self.labelDelete];
    
    self.labelMore = [UILabel new];
    self.labelMore.font = [UIFont systemFontOfSize:14.0f];
    self.labelMore.textColor = [UIColor grayColor];
    self.labelMore.userInteractionEnabled = YES;
    UITapGestureRecognizer *moreTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMoreTap)];
    [self.labelMore addGestureRecognizer:moreTap];
    [self.contentView addSubview:self.labelMore];
    
    
    self.picContainerView = [[YHWorkGroupPhotoContainer alloc] initWithWidth:MAIN_SCREEN_WIDTH-20 - 60];
    [self.contentView addSubview:self.picContainerView];
    
    
    self.bottomView = [[UIView alloc]init];
    self.labelBottomLeft = [UILabel new];
    self.labelBottomLeft.font = [UIFont systemFontOfSize:14.0f];
    self.labelBottomLeft.textColor = [UIColor grayColor];
    [self.bottomView addSubview:self.labelBottomLeft];
    
    self.labelBottomRight = [UILabel new];
    self.labelBottomRight.font = [UIFont systemFontOfSize:14.0f];
    self.labelBottomRight.textColor = UIColors(0x576b95);
    [self.bottomView addSubview:self.labelBottomRight];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = UIColors(0xEFEFEF);
    [self.bottomView addSubview:self.lineView];
    
    [self.contentView addSubview:self.bottomView];
    
    [self layoutUI];
}

-(void)hiddenYincang
{
    [self.labelDelete setHidden:YES];
}

//-(void)ChangeLayout
//{
//    [self.imgvAvatar mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.contentView).offset(15);
//        make.left.equalTo(self.contentView).offset(7);
//        make.width.height.mas_equalTo(0);
//    }];
//}

- (void)layoutUI{
    __weak typeof(self)weakSelf = self;
    [self.imgvAvatar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(15);
        make.left.equalTo(weakSelf.contentView).offset(7);
        make.width.height.mas_equalTo(45);
    }];
    
    [self.labelName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.contentView).offset(15);
        make.left.equalTo(weakSelf.imgvAvatar.mas_right).offset(8);
        make.right.equalTo(weakSelf.labelPubTime.mas_left).offset(0);
    }];
    
    [self.labelPubTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weakSelf.labelName.mas_bottom);
        make.right.equalTo(weakSelf.contentView).offset(-15);
        make.top.equalTo(weakSelf.contentView).offset(15);
    }];

    [self.labelPubTime setContentHuggingPriority:251 forAxis:UILayoutConstraintAxisHorizontal];
    [self.labelPubTime setContentCompressionResistancePriority:751 forAxis:UILayoutConstraintAxisHorizontal];
    
    _cstHeightlbContent = [NSLayoutConstraint constraintWithItem:self.labelContent attribute:NSLayoutAttributeHeight relatedBy:0 toItem:nil attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
    [self.contentView addConstraint:_cstHeightlbContent];


    [self.labelContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.imgvAvatar.mas_bottom).offset(-20);
        make.left.equalTo(weakSelf.contentView).offset(60);
        make.right.equalTo(weakSelf.contentView).offset(-10);
        make.bottom.equalTo(weakSelf.labelMore.mas_top).offset(-11);
    }];
    
    // 不然在6/6plus上就不准确了
    self.labelContent.preferredMaxLayoutWidth = MAIN_SCREEN_WIDTH-20;
    
    _cstHeightlbMore = [NSLayoutConstraint constraintWithItem:self.labelMore attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:10];
    [self.contentView addConstraint:_cstHeightlbMore];
    [self.labelMore mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.labelContent.mas_bottom).offset(5);
        make.left.equalTo(weakSelf.contentView).offset(60);
        make.width.mas_equalTo(80);
    }];
    
    
    _cstHeightlbDelete = [NSLayoutConstraint constraintWithItem:self.labelDelete attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.contentView addConstraint:_cstHeightlbDelete];
    _cstCenterYlbDelete = [NSLayoutConstraint constraintWithItem:self.labelDelete attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.labelMore attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
    [self.contentView addConstraint:_cstCenterYlbDelete];
    _cstLeftlbDelete    = [NSLayoutConstraint constraintWithItem:self.labelDelete attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.labelMore attribute:NSLayoutAttributeRight multiplier:1.0 constant:10];
    [self.contentView addConstraint:_cstLeftlbDelete];
    [self.labelDelete mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(80);
    }];
    
    _cstHeightPicContainer = [NSLayoutConstraint constraintWithItem:self.picContainerView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];
    [self.contentView addConstraint:_cstHeightPicContainer];
    _cstTopPicContainer = [NSLayoutConstraint constraintWithItem:self.picContainerView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.labelMore attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10];
    [self.contentView addConstraint:_cstTopPicContainer];
    [self.picContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView).offset(60);
        make.right.mas_greaterThanOrEqualTo(weakSelf.contentView).offset(-10);
    }];
    [self.picContainerView setContentHuggingPriority:249 forAxis:UILayoutConstraintAxisVertical];
    [self.picContainerView setContentCompressionResistancePriority:749 forAxis:UILayoutConstraintAxisVertical];
    
    _cstTopViewBottom = [NSLayoutConstraint constraintWithItem:self.bottomView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.picContainerView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    
    _cstTopViewBottom.priority = UILayoutPriorityDefaultLow;
    [self.contentView addConstraint:_cstTopViewBottom];
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(60);
        make.right.equalTo(weakSelf.contentView.mas_right);
        make.height.mas_equalTo(44);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.contentView.mas_left).offset(-60);
        make.height.mas_equalTo(1);
        make.right.equalTo(weakSelf.contentView.mas_right);
        make.bottom.equalTo(weakSelf.contentView.mas_bottom);
    }];
    
    [self.labelBottomLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bottomView.mas_left);
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];
    
    [self.labelBottomRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bottomView.mas_right).offset(-10);
        make.centerY.equalTo(self.bottomView.mas_centerY);
    }];

    
    /*******使用HYBMasonryAutoCell*******/
    [self.viewSeparator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.bottomView.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(15);
    }];
    
    self.hyb_lastViewInCell = self.viewSeparator;
    
}


- (void)setModel:(YHWorkGroup *)model
{
    _model = model;
    [self.imgvAvatar setImage:[UIImage imageNamed:@"avatar"]];
    [self.imgvAvatar sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KUrl,model.avatar]] placeholderImage:Kplaceholderimage];
    self.labelName.text     = _model.title;
    self.labelPubTime.text  = @"";
    /*************动态内容*************/
    maxContentLabelHeight   = _labelContent.font.pointSize * 8;
    self.labelContent.text  = _model.msgContent;

    //查看详情按钮
    self.labelMore.text     = @"查看全部";
    CGFloat moreBtnH = 0;

    
    if (_model.shouldShowMoreButton) { // 如果文字高度超过60
        moreBtnH = moreBtnHeight;

        if (_model.isOpening) { // 如果需要展开
            _labelMore.text = @"收起";
            _cstHeightlbContent.constant = [AdjustHeight adjustHeightByString:_labelContent.text width:KscreenW-70 font:14];
        } else
        {
            _labelMore.text = @"查看全部";
            _cstHeightlbContent.constant = 40.f;
        }
    }else{
        _cstHeightlbContent.constant = [AdjustHeight adjustHeightByString:_labelContent.text width:KscreenW-70 font:14];
    }
    
    //删除按钮
    self.labelDelete.text   = @"删除";
    CGFloat delBtnH = 0;

    if (model.showDeleteButton)
    {
        delBtnH = deleteBtnHeight;
    }else
    {

    }
    
    //更新“查看详情”和“删除按钮”的约束
    _cstHeightlbMore.constant   = moreBtnH;
    _cstHeightlbDelete.constant = delBtnH;
    
    if (moreBtnH) {
        _cstLeftlbDelete.constant    = 10;
        _cstCenterYlbDelete.constant = 0;
    }else{
        _cstLeftlbDelete.constant    = -80;
        _cstCenterYlbDelete.constant = 11;
    }
    
    CGFloat picTop = 0;
    if (moreBtnH) {
        picTop = 10;
    }else if(delBtnH && !moreBtnH){
        picTop = 30;
    }else{
        picTop = 0;
    }
    
    _cstTopPicContainer.constant    = picTop;
    
    CGFloat picContainerH = [self.picContainerView setupPicUrlArray:_model.thumbnailPicUrls];
    self.picContainerView.picOriArray = _model.originalPicUrls;
    
    _cstHeightPicContainer.constant = picContainerH;
    
    CGFloat viewBottomTop = 0;
    if(_model.thumbnailPicUrls.count){
        viewBottomTop = 5;
    }
    _cstTopViewBottom.constant = viewBottomTop;
    
    
    [self.labelBottomLeft setText:_model.bottomLeft];
    [self.labelBottomRight setText:_model.bottomRight];
    

}

#pragma mark - Action
- (void)onMoreTap
{
    
    if (_delegate && [_delegate respondsToSelector:@selector(onMoreInCell:)]) {
        [_delegate onMoreInCell:self];
    }
}

- (void)deleteTap{
    
    if (_delegate && [_delegate respondsToSelector:@selector(onDeleteInCell:)]) {
        [_delegate onDeleteInCell:self];
    }
}

#pragma mark - Gesture

- (void)onAvatar:(UITapGestureRecognizer *)recognizer{
    
    if(recognizer.state == UIGestureRecognizerStateEnded){
        if (_delegate && [_delegate respondsToSelector:@selector(onAvatarInCell:)]) {
            [_delegate onAvatarInCell:self];
        }
    }
}

#pragma mark - HKPBotViewDelegate
- (void)onAvatar{
    
}

- (void)onMore{
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
